package com.jerry.controller;

import com.jerry.DTO.FileChunkDTO;
import com.jerry.response.CommonResponse;
import com.jerry.service.FileService;
import com.jerry.service.Impl.FileServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/file")
public class FileController {
    private final FileService fileService;

    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    @PostMapping("/write")
    public CommonResponse write(@Valid @RequestBody FileChunkDTO fileChunkDTO) {
        String fileMd5 = fileService.write(fileChunkDTO);
        return CommonResponse.success(fileMd5);
    }

    @GetMapping("/read")
    public CommonResponse read(@RequestParam("filename") String filename,
                               @RequestParam("extension") String extension,
                               @RequestParam("bucketName") String bucketName,
                               @RequestParam("chunkNo") Integer chunkNo) {
        byte[] bytes = fileService.read(filename, extension, bucketName, chunkNo);
        return CommonResponse.success(bytes);
    }
    @DeleteMapping("/delete")
    public CommonResponse delete(@RequestParam("filename") String filename,
                                 @RequestParam("extension") String extension,
                                 @RequestParam("bucketName") String bucketName,
                                 @RequestParam("chunkNo") Integer chunkNo){
        fileService.delete(filename, extension, bucketName, chunkNo);
        return CommonResponse.success();
    }
}
